package com.wangxiang.shop.product.controller;

import java.util.HashMap;
import java.util.List;


import com.wangxiang.common.utils.PageEntity;
import com.wangxiang.common.utils.SearchEntity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wangxiang.shop.product.entity.ClothingSpuEntity;
import com.wangxiang.shop.product.service.ClothingSpuService;
import com.wangxiang.common.utils.R;



/**
 *
 *
 * @author wangxiang
 * @email 2293672863@qq.com
 * @date 2022-03-14 11:30:20
 */
@RestController
@RequestMapping("product/clothingspu")
public class ClothingSpuController {
    @Autowired
    private ClothingSpuService clothingSpuService;


    /**
     * 分页
     * @param pageEntity
     * @return
     */
    @RequestMapping("page")
    public R page(@RequestBody PageEntity pageEntity){
        PageEntity page = clothingSpuService.page(pageEntity);
        return R.ok().put("page",page);
    }
    //模糊查询
    @RequestMapping("search")
    public R search(@RequestBody PageEntity pageEntity){
       if (pageEntity.getLike()==null){
           return R.ok().put("msg","你还没有输入要查询的商品").put("code",700);
       }
        PageEntity search = clothingSpuService.search(pageEntity);
       return R.ok().put("page",search);

    }

    /**
     * 修改
     * @param clothingSpuEntity
     * @return
     */
    @RequestMapping("update")
    public R change(@RequestBody ClothingSpuEntity clothingSpuEntity){
        System.out.println("数据"+clothingSpuEntity);
        if (clothingSpuEntity.getId() == 0){
            return R.error("系统错误");
        }

        int change = clothingSpuService.change(clothingSpuEntity);
        if (change==0){
            return R.error("修改错误");
        }
        return R.ok().put("status",1).put("msg","修改成功："+clothingSpuEntity).put("code",800);
    }
    @RequestMapping("get")
    public R getEntity(@RequestBody ClothingSpuEntity clothingSpuEntity){
        if (clothingSpuEntity.getId() == 0) {
            return R.error("查询错误");
        }

        ClothingSpuEntity entity = clothingSpuService.getEntity(clothingSpuEntity);
        if (entity == null){
            return R.ok().put("msg","该商品已经下架，或不存在").put("code",700);
        }
        return R.ok().put("data",entity);
    }


    /**
     * 失效商品，涉及sku的失效
     * @param clothingSpuEntity
     * @return
     */
    @RequestMapping("invalid")
    public R invalid(@RequestBody ClothingSpuEntity clothingSpuEntity){
        int invalid = clothingSpuService.invalid(clothingSpuEntity);
        if (invalid==1){
            return R.error("库存失效失败，请联系管理员处理");
        }
        if (invalid == 2){
            return R.error("商品失效失败，系统异常");
        }
        return R.ok().put("status",invalid);
    }


}
